﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using SX.Utils;
using SX.Utils.Dapper;

namespace Ly.Scheduler.Common
{
    public static class CommonDao
    {
        public const string QYWX_SEND_MESSAG_API = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
        private static string _PDCA_CATEGORY_SALES_ID = Guid.Empty.ToString(); //"dfc4c231-f49d-43f4-b160-1417850778e8";

        ///// <summary>
        ///// 营销类目Id
        ///// </summary>
        //public static string PDCA_CATEGORY_SALES_ID
        //{
        //    get
        //    {

        //        if (_PDCA_CATEGORY_SALES_ID == Guid.Empty.ToString())
        //        {
        //            _PDCA_CATEGORY_SALES_ID = GetSysConfigValue("PDCA_CATEGORY_SALES_ID");
        //        }  
        //        return _PDCA_CATEGORY_SALES_ID;
        //    }

        //} //= "dfc4c231-f49d-43f4-b160-1417850778e8"; // 

        ///// <summary>
        /////  通过HeId 获取LoginName
        ///// </summary>
        ///// <param name="heId">员工Id</param>
        ///// <returns>System.String.</returns>
        //public static string GetLoginNameByHeId(Guid heId)
        //{
        //    string sql = "SELECT TOP 1 LoginName FROM dbo.tbl_SysUsers(nolock) WHERE heID =@HeId";
        //    using (var conn = DaoHelper.GetConnectionDb1())
        //    {
        //        return conn.ExecuteScalar<string>(sql,new {HeId = heId});
        //    } 
        //}

        ///// <summary>
        /////  通过企业号我的消息发送信息
        ///// </summary>
        ///// <param name="who">接收人</param>
        ///// <param name="message">消息内容</param>
        ///// <returns>System.Int32.</returns>
        //public static int SendMsg2Wx(string who, string message)
        //{
        //    var appId = ConfigHelper.GetApp("WXSendMessageAPP");
        //    if (string.IsNullOrEmpty(appId))
        //    {
        //        appId = "46";
        //    }
        //    string msg = JsonHelper.ToJson(new
        //    {
        //        touser = who,
        //        toparty = "",
        //        msgtype = "text",
        //        agentid = CommonHelper.ToInt(appId, 46),
        //        text = new { content = message },
        //        safe = 0
        //    });

        //    StringBuilder strSql = new StringBuilder();
        //    strSql.AppendFormat("insert into [tbl_sysSendMsg4Weixin] ([uri],[txt],[PlanSendTime],  IsSend, errcode");
        //    strSql.Append(") values (");
        //    strSql.AppendFormat("@uri,@txt,getdate(),0,0)");
        //    using (var conn = DaoHelper.GetConnectionDb1())
        //    {
        //        return conn.Execute(strSql.ToString(), new {uri = QYWX_SEND_MESSAG_API, txt = msg});
        //    } 
        //}
        ///// <summary>
        /////  通过企业号我的消息发送信息
        ///// </summary>
        ///// <param name="who">接收人</param>
        ///// <param name="news">文章列表</param>
        ///// <returns>System.Int32.</returns>
        //public static int SendNewsMsg2Wx(string who, WXNewsArticle news)
        //{
        //    return SendNewsMsg2Wx(who, new List<WXNewsArticle> { news });
        //}
        ///// <summary>
        /////  通过企业号我的消息发送信息
        ///// </summary>
        ///// <param name="who">接收人</param>
        ///// <param name="news">文章列表</param>
        ///// <returns>System.Int32.</returns>
        //public static int SendNewsMsg2Wx(string who, List<WXNewsArticle> news)
        //{
        //    var appId = ConfigHelper.GetApp("WXSendMessageAPP");
        //    if (string.IsNullOrEmpty(appId))
        //    {
        //        appId = "46";
        //    }
        //    string msg = JsonHelper.ToJson(new
        //    {
        //        touser = who,
        //        toparty = "",
        //        msgtype = "news",
        //        agentid = CommonHelper.ToInt(appId, 46),
        //        news = new { articles = news },

        //    });

        //    StringBuilder strSql = new StringBuilder();
        //    strSql.AppendFormat("insert into [tbl_sysSendMsg4Weixin] ([uri],[txt],[PlanSendTime],  IsSend, errcode");
        //    strSql.Append(") values (");
        //    strSql.AppendFormat("@uri,@txt,getdate(),0,0)");
        //    using (var conn = DaoHelper.GetConnectionDb1())
        //    {
        //        return conn.Execute(strSql.ToString(), new { uri = QYWX_SEND_MESSAG_API, txt = msg });
        //    }
        //}

        /////// <summary>
        /////// 获取进项税默认值
        /////// </summary>
        /////// <returns></returns>
        ////public static float GetInputTaxDefaultValue()
        ////{ 
        ////    StringBuilder strSql = new StringBuilder();
        ////    strSql.AppendFormat("select top 1 sysvalue from tbl_SysConfig(nolock) where syskey='INPUTTAX_DEFAULT'"); 
        ////    using (var conn = DaoHelper.GetConnectionDb1())
        ////    {
        ////        return CommonHelper.ToFloat(conn.ExecuteScalar<string>(strSql.ToString()),(float)0.17);
        ////    }
        ////}

        ///// <summary>
        ///// 获取系统配置
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //public static string GetSysConfigValue(string key)
        //{
        //    using (var conn = DaoHelper.GetConnectionDb1())
        //    {
        //        return conn.ExecuteScalar<string>(
        //            "SELECT TOP 1 [sysvalue]    FROM  [dbo].[tbl_SysConfig] WHERE syskey =@syskey", new {syskey = key});
        //    }
        //}
    }

    public class WXNewsArticle
    {
        public string title { get; set; }
        public string description { get; set; }
        public string url { get; set; }
        public string picurl { get; set; }
    }
}
